package PHANVANVUONG.XML.HOMEWORK.EX01;

import java.awt.BorderLayout;
import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.border.TitledBorder;
import javax.swing.filechooser.FileNameExtensionFilter;

import java.awt.Color;
import javax.swing.JLabel;
import javax.swing.JOptionPane;

import java.awt.Font;
import javax.swing.JTextField;
import javax.swing.JRadioButton;
import javax.swing.ButtonGroup;
import com.toedter.calendar.JDateChooser;
import javax.swing.JButton;
import javax.swing.JFileChooser;

import java.awt.event.ActionListener;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;
import java.awt.event.ActionEvent;
import java.awt.event.ItemListener;
import java.awt.event.ItemEvent;

@SuppressWarnings({ "unused", "serial" })
public class GuiNhapThongTin extends JFrame {

	private JPanel contentPane;
	private JTextField maSV;
	private JTextField fullName;
	private JTextField phoneNo;
	private JTextField SSN;
	private JTextField ketQua;
	private JTextField email;
	private final ButtonGroup buttonGroup = new ButtonGroup();
	JDateChooser birthDay;
	private String sex = "";
	List<Object> lisField;
	List<List<Object>> lisValues;
	List<Object> lisValuField;
	JRadioButton rbNam;
	JRadioButton rbNu;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					GuiNhapThongTin frame = new GuiNhapThongTin();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public GuiNhapThongTin() {
		setTitle("Nhập thông tin sinh viên");
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 629, 486);
		contentPane = new JPanel();
		contentPane.setBorder(new TitledBorder(null, "X\u1EED l\u00FD th\u00F4ng tin", TitledBorder.LEADING,
				TitledBorder.TOP, null, Color.RED));
		setContentPane(contentPane);
		contentPane.setLayout(null);

		JLabel lblNewLabel = new JLabel("Mã sinh viên");
		lblNewLabel.setFont(new Font("Cambria", Font.PLAIN, 14));
		lblNewLabel.setBounds(30, 35, 89, 18);
		contentPane.add(lblNewLabel);

		JLabel lblHVTn = new JLabel("Họ và tên");
		lblHVTn.setFont(new Font("Cambria", Font.PLAIN, 14));
		lblHVTn.setBounds(30, 80, 89, 18);
		contentPane.add(lblHVTn);

		JLabel lblGiiTnh = new JLabel("Giới tính");
		lblGiiTnh.setFont(new Font("Cambria", Font.PLAIN, 14));
		lblGiiTnh.setBounds(30, 125, 89, 18);
		contentPane.add(lblGiiTnh);

		JLabel lblNgySinh = new JLabel("Ngày sinh");
		lblNgySinh.setFont(new Font("Cambria", Font.PLAIN, 14));
		lblNgySinh.setBounds(30, 170, 89, 18);
		contentPane.add(lblNgySinh);

		JLabel lblinThoi = new JLabel("Điện thoại");
		lblinThoi.setFont(new Font("Cambria", Font.PLAIN, 14));
		lblinThoi.setBounds(30, 215, 89, 18);
		contentPane.add(lblinThoi);

		JLabel lblSCmnd = new JLabel("Số CMND");
		lblSCmnd.setFont(new Font("Cambria", Font.PLAIN, 14));
		lblSCmnd.setBounds(30, 260, 89, 18);
		contentPane.add(lblSCmnd);

		JLabel lblKtQu = new JLabel("Kết quả");
		lblKtQu.setFont(new Font("Cambria", Font.PLAIN, 14));
		lblKtQu.setBounds(30, 305, 89, 18);
		contentPane.add(lblKtQu);

		JLabel lblSinThoi = new JLabel("Email");
		lblSinThoi.setFont(new Font("Cambria", Font.PLAIN, 14));
		lblSinThoi.setBounds(349, 215, 44, 18);
		contentPane.add(lblSinThoi);

		maSV = new JTextField();
		maSV.setBounds(129, 35, 192, 20);
		contentPane.add(maSV);
		maSV.setColumns(10);

		fullName = new JTextField();
		fullName.setColumns(10);
		fullName.setBounds(129, 80, 192, 20);
		contentPane.add(fullName);

		phoneNo = new JTextField();
		phoneNo.setColumns(10);
		phoneNo.setBounds(129, 215, 192, 20);
		contentPane.add(phoneNo);

		SSN = new JTextField();
		SSN.setColumns(10);
		SSN.setBounds(129, 260, 192, 20);
		contentPane.add(SSN);

		ketQua = new JTextField();
		ketQua.setEditable(false);
		ketQua.setColumns(10);
		ketQua.setBounds(129, 305, 450, 75);
		contentPane.add(ketQua);

		email = new JTextField();
		email.setColumns(10);
		email.setBounds(398, 215, 181, 20);
		contentPane.add(email);

		rbNam = new JRadioButton("Nam");
		rbNam.addItemListener(new ItemListener() {
			@SuppressWarnings("deprecation")
			public void itemStateChanged(ItemEvent arg0) {
				sex=rbNam.getLabel();
			}
		});
		buttonGroup.add(rbNam);
		rbNam.setFont(new Font("Cambria", Font.PLAIN, 14));
		rbNam.setBounds(129, 124, 75, 23);
		contentPane.add(rbNam);

		rbNu = new JRadioButton("Nữ");
		rbNu.addItemListener(new ItemListener() {
			@SuppressWarnings("deprecation")
			public void itemStateChanged(ItemEvent e) {
				sex=rbNu.getLabel();
			}
		});
		buttonGroup.add(rbNu);
		rbNu.setFont(new Font("Cambria", Font.PLAIN, 14));
		rbNu.setBounds(206, 124, 75, 23);
		contentPane.add(rbNu);

		birthDay = new JDateChooser();
		birthDay.setDateFormatString("dd/MM/yyyy");
		birthDay.setBounds(129, 170, 118, 20);
		contentPane.add(birthDay);

		lisField = new ArrayList<Object>();
		lisField.add("MaSV");
		lisField.add("HoVaTen");
		lisField.add("GioiTinh");
		lisField.add("NgaySinh");
		lisField.add("DienThoai");
		lisField.add("Email");
		lisField.add("CMND");
		
		
		lisValues = new ArrayList<List<Object>>();

		JButton btnNewButton = new JButton("Ghi dữ liệu nối tiếp vào XML");
		btnNewButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				JFileChooser objFile = new JFileChooser();
                objFile.setCurrentDirectory(new java.io.File("."));
                objFile.setDialogTitle("Chọn file: ");
                objFile.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
                String[] EXTENSION = new String[] { "xml" };
                FileNameExtensionFilter filter = new FileNameExtensionFilter("XML files", EXTENSION);
                objFile.setFileFilter(filter);
                objFile.setMultiSelectionEnabled(false);

                int returnVal = objFile.showSaveDialog(getParent());
                String ParentPath = "";
                String FileName = "";
                if (returnVal == JFileChooser.APPROVE_OPTION) {
                    ParentPath = objFile.getSelectedFile().getParent();
                    FileName = objFile.getSelectedFile().getName();
                }
                
                Controllers objC=new Controllers();
                boolean b;
                try{
                	b=objC.WriteFileXML(lisField, lisValues, "School", "Student", ParentPath, FileName, false);
                }catch(Exception e){
                	e.printStackTrace();
                	b=false;
                }
                
                if(b){
                	ketQua.setText("Ghi nối file XML thành công!!!");
                	lisValues.clear();
                }else{
                	ketQua.setText("Ghi nối file thất bại");
                }
			}
		});
		btnNewButton.setFont(new Font("Cambria", Font.PLAIN, 14));
		btnNewButton.setBounds(129, 403, 152, 23);
		contentPane.add(btnNewButton);

		JButton btnGhiDLiu = new JButton("Ghi dữ liệu vào XML");
		btnGhiDLiu.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				JFileChooser objFile = new JFileChooser();
                objFile.setCurrentDirectory(new java.io.File("."));
                objFile.setDialogTitle("Chọn file: ");
                objFile.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
                String[] EXTENSION = new String[] { "xml" };
                FileNameExtensionFilter filter = new FileNameExtensionFilter("XML files", EXTENSION);
                objFile.setFileFilter(filter);
                objFile.setMultiSelectionEnabled(false);

                int returnVal = objFile.showSaveDialog(getParent());
                String ParentPath = "";
                String FileName = "";
                if (returnVal == JFileChooser.APPROVE_OPTION) {
                    ParentPath = objFile.getSelectedFile().getParent();
                    FileName = objFile.getSelectedFile().getName();
                }
                
                Controllers objC=new Controllers();
                boolean b;
                try{
                	b=objC.WriteFileXML(lisField, lisValues, "School", "Student", ParentPath, FileName, true);
                }catch(Exception e){
                	e.printStackTrace();
                	b=false;
                }
                
                if(b){
                	ketQua.setText("Ghi xuống file XML thành công");
                	lisValues.clear();
                }else{
                	ketQua.setText("Ghi file XML thất bại");
                }
                
			}
		});
		btnGhiDLiu.setFont(new Font("Cambria", Font.PLAIN, 14));
		btnGhiDLiu.setBounds(291, 403, 152, 23);
		contentPane.add(btnGhiDLiu);
		
		
		JButton btnThmThngTin = new JButton("Thêm thông tin");
		btnThmThngTin.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				Controllers objC = new Controllers();
				//lisValuField=new ArrayList<Object>();
				if (maSV.getText().equals("") || fullName.getText().equals("") || sex.equals("")
						|| birthDay.equals(null) || phoneNo.getText().equals("") || email.getText().equals("")
						|| SSN.getText().equals("")) {
					JOptionPane.showMessageDialog(getParent(), "Vui lòng nhập đủ thông tin!!!", "Thông báo",
							JOptionPane.ERROR_MESSAGE);
				} else if (!objC.checkMasv(maSV.getText()) || phoneNo.getText().length() < 10
						|| phoneNo.getText().length() > 11 || !objC.checkEmail(email.getText())
						|| SSN.getText().length() != 9) {
					JOptionPane.showMessageDialog(getParent(), "Vui lòng kiểm tra lại thông tin!!!", "Thông báo",
							JOptionPane.ERROR_MESSAGE);
				}else{
					lisValuField=new ArrayList<Object>();
					lisValuField.add(maSV.getText());
					lisValuField.add(fullName.getText());
					lisValuField.add(sex);
					lisValuField.add(new SimpleDateFormat("dd/MM/yyyy").format(birthDay.getDate()));
					lisValuField.add(phoneNo.getText());
					lisValuField.add(email.getText());
					lisValuField.add(SSN.getText());
					
					lisValues.add(lisValuField);
					ketQua.setText("Có: "+lisValues.size()+" sinh viên trong danh sách");
					
					maSV.setText("");
					fullName.setText("");
					birthDay.setDate(null);
					phoneNo.setText("");
					email.setText("");
					SSN.setText("");
				}
			}
		});
		btnThmThngTin.setFont(new Font("Cambria", Font.PLAIN, 14));
		btnThmThngTin.setBounds(453, 403, 126, 23);
		contentPane.add(btnThmThngTin);
	}
}
